package com.zzld.modules.autoinn.action;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.github.pagehelper.PageInfo;
import com.zzld.core.common.config.GlobalStatic;
import com.zzld.core.sms.SenderSms;
import com.zzld.core.utils.Base64Coder;
import com.zzld.core.utils.CookieUtils;
import com.zzld.core.utils.UploadFileUtil;
import com.zzld.core.utils.UuidUtil;
import com.zzld.modules.autoinn.entity.Customer;
import com.zzld.modules.autoinn.entity.UserRegister;
import com.zzld.modules.autoinn.service.ICustomerService;
import com.zzld.modules.autoinn.service.IUserRegisterService;
import com.zzld.modules.common.dto.CommonJSON;


@Controller
@RequestMapping("/weixin/f/autoinn/bx/userRegister")
public class WXUserRegisterController {

	@Autowired
	private ICustomerService customerService;

	
	@Autowired
	private IUserRegisterService userregisterService;

	private String listurl = "/wx/autoinn/wxuserRegister";	
	private String tiok="/wx/autoinn/registerti";	
	private String tino="/wx/autoinn/registerno";
	//private String tierror="/wx/autoinn/Registererror";

	@RequestMapping(method = RequestMethod.GET)
	public String liq() {
		return listurl;
	}
	
  

	
	


	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String add(Model model, HttpServletRequest request) {
	
	    String openid = CookieUtils.getCookie(request, "autoinn_session");
		String wxaccount = CookieUtils.getCookie(request, "wx_session"); 
		
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
		try {
			
		
		    
			openid = Base64Coder.decode(openid);
			wxaccount = Base64Coder.decode(wxaccount);
			
			Customer customer = customerService.findCustomer(wxaccount, openid);
			
			
			UserRegister register = new UserRegister();
			register.setId(UuidUtil.uuid().toString());
			register.setBrand(request.getParameter("brand"));
		    register.setMotorcycle(request.getParameter("motorcycle"));
		    register.setNickname(request.getParameter("nickname"));
		    register.setPhone(request.getParameter("phone"));
		    register.setWxaccount(wxaccount);
		    register.setOpenid(openid);
		    register.setCreateime(new Date());
			register.setCustomerId(customer.getCustomerId());
			List<Map<String, Object>> upload_list = UploadFileUtil.uploadFile(
					request, "baoxian" + File.separator, "gif,jpg,bmp,jpeg,png");
			if (upload_list != null && upload_list.size() == 2) {

				System.out.println("一共有"+upload_list.size());
				
						   Map<String, Object> upload_map = upload_list.get(0);
						   Map<String, Object> upload_map1 = upload_list.get(1);
						    boolean iserror = upload_map.containsKey("error");
						    boolean iserror1 = upload_map1.containsKey("error");
							if (iserror || iserror1) {
								 
							
							} else {
							
						    	String yqq = (String) upload_map.get("fileName");
						        register.setImgeone(yqq);
						        register.setImgetwo(upload_list.get(1).get("fileName").toString());
							}
	
			}else{
				return tino;
			}
			
			if(userregisterService.saveSelective(register)>0){
				
			}
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			return tino;
		}
		return tiok;
		
	
	}

	
	
	



	
	

}
